package Hot100;

import java.util.ArrayList;
import java.util.List;

public class 全排列 {

    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int[] nums;
    int len;
    boolean[] selected;

    public List<List<Integer>> permute(int[] nums) {
        this.nums = nums;
        len = nums.length;
        selected = new boolean[len];
        backTracking(0);
        return res;
    }

    private void backTracking(int pathLen) {
        if (pathLen == len) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < len; i++) {
            if (selected[i]) continue;
            path.add(nums[i]);
            selected[i] = true;
            backTracking(pathLen + 1);
            path.removeLast();
            selected[i] = false;
        }
    }
}
